using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;


namespace SpaceEventManager
{
    public class SpaceManager
    {
        Dictionary<string, Space> dictAllSpacesNameKey;

        public SpaceManager()
        {
            dictAllSpacesNameKey = new Dictionary<string, Space>();
        }
        

        public Dictionary<string, Space> DictAllSpacesNameKey
        { get { return dictAllSpacesNameKey; } set { dictAllSpacesNameKey = value; } }


        /*
         * Searches dictAllSpacesNameKey's keys for any key that starts
         * with the string passed in. Returns a list of the items that
         * match. */
        public List<string> SearchAllSpaces(string name)
        {
            List<string> foundSpaces = new List<string>();
            foreach (string s in dictAllSpacesNameKey.Keys)
            {
                if (s.Contains(name))
                {
                    foundSpaces.Add(s);
                }
            }
            return (foundSpaces);
        }

        /*
         * Adds a space to dictAllSpacesNameKey, so long as dictAllSpacesNameKey
         * does not already contain a value with the same name. */
        public void AddSpace(Space iSpace)
        {
            if ((iSpace != null) && !(dictAllSpacesNameKey.ContainsKey(iSpace.Name)))
            {
                dictAllSpacesNameKey.Add(iSpace.Name, iSpace);
            }
        }

        /*
         * Removes a space from dictAllSpacesNameKey.
         * RemoveSpace must not only remove the space from the main list of spaces,
         * it must also remove all occurences of the space from the other spaces' 
         * InnerSpaces lists.*/
        public void RemoveSpace(Space iSpace)
        {
            if (iSpace != null)
            {
                if(dictAllSpacesNameKey.ContainsValue(iSpace))
                {
                    dictAllSpacesNameKey.Remove(iSpace.Name);

                    foreach (Space s in dictAllSpacesNameKey.Values)
                    {
                        if (s.InnerSpaces.ContainsKey(iSpace))
                        {
                            s.RemoveSpace(iSpace);
                        }
                    }
                }
            }
        }

        /*
         * UpdateSpace takes in a space that should be changed.
         * It must change all the spaces who have the space as 
         * an InnerSpace, as well as update the main lists. */
        public void UpdateSpace(Space updatedSpace)
        {
            string spaceName = updatedSpace.Name;
            int countInnerSpaces = updatedSpace.InnerSpaces.Count;

            if (dictAllSpacesNameKey.ContainsKey(spaceName))
            {
                Space toBeUpdated = dictAllSpacesNameKey[spaceName];


                //runs through all the spaces in dictAllSpacesNameKey,
                //searches all of their innerSpaces for the space that
                //needs to be updated, and updates them.
                foreach (Space s in dictAllSpacesNameKey.Values)
                {
                    foreach (Space iS in s.InnerSpaces.Keys)
                    {
                        if (iS.Name == spaceName)
                        {
                            Point tempLoc = s.InnerSpaces[iS];
                            s.RemoveSpace(iS);
                            s.InnerSpaces.Add(updatedSpace, tempLoc);
                            break;
                        }
                    }
                }

                /*
                foreach (Space s in dictAllSpacesNameKey.Values)
                {
                    foreach (Space iS in s.InnerSpaces.Keys)
                    {
                        if (iS.Name == spaceName)
                        {
                            Point tempLoc = s.InnerSpaces[iS];
                            s.RemoveSpace(iS);
                            s.InnerSpaces.Add(updatedSpace, tempLoc);
                            break;
                        }
                    }
                }
                */

                dictAllSpacesNameKey[spaceName] = updatedSpace;
                int count = dictAllSpacesNameKey[spaceName].InnerSpaces.Count;

            }
        }
    }
}
